package com.briup.exception;

import com.briup.result.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Project: jd2407_third
 * Package: com.briup.exception
 * Author: XBai
 * Time: 2024/10/30 9:29
 * 全局异常处理 用来增强controller返回json格式错误信息的功能
 */

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public Object handleException(Exception e) {
        //定义返回的结果，和Controller一样
        Result<?> result = null;
        if (e instanceof ServiceException){
            result = Result.failure(((ServiceException) e).getResultCode());
        }else {
            result = Result.failure(500,"服务器未知错误:" + e.getMessage());
        }

        return result;
    }
}
